﻿using System;
using System.Collections.Generic;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.Core.Web;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json.Linq;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    public class PageCommon_Button:ControlBase, ICommandElement
    {
        private string _title = "";
        private ColorPaletteEnum _color = ColorPaletteEnum.@default;
        private string _iconHtml = "";

        private ButtonTypeEnum _type = ButtonTypeEnum.button;
        //private string _href = "";

        public PageCommon_Button(ViewContext viewContext) : base(viewContext)
        {
            this.Css("btn");
        }
        
        public ButtonCmdBase Command { set; get; }
        public string ReommentTag => _type == ButtonTypeEnum.link ? "a" : "button";

        public PageCommon_Button OnClick(string onclick)
        {
            Attributes.SetOrReplaceAttr("onclick", onclick);

            return this;
        }

        public PageCommon_Button Type(ButtonTypeEnum type)
        {
            _type = type;
            Attributes.SetOrReplaceAttr("type", type.ToStringEx());

            return this;
        }

        public PageCommon_Button Text(string text) 
        {
            _title = text;

            return this;
        }
               
        public PageCommon_Button Text(Func<PageCommon_Button, HelperResult> text)
        {
            _title = text(this).ToHtmlString();

            return this;
        }
               
        public PageCommon_Button Color(ColorPaletteEnum color)
        {
            _color = color;

            return this;
        }
               
        public PageCommon_Button IconCss(string css)
        {
            _iconHtml = $"<i class=\"fa {css}\"></i>";

            return this;
        }
               
        public PageCommon_Button DisabledAfterClick(bool isAutoDisabled = true)
        {
            Attributes.Data("data-disabledafterclick", isAutoDisabled.ToStringEx());

            return this;
        }

        //public PageCommon_Button ShowModal(string url, string onSuccessCallback = "", string onCloseCallback = "")
        //{
        //    this.Command = new ButtonCmd_ShowModal().SetUrl(url).SetOnSuccessCallback(onSuccessCallback).SetOnCloseModalCallback(onCloseCallback);

        //    return this;
        //}

        //public PageCommon_Button ShowDataPicker(string url, string onSelectedCallback, string onShowCallback = "", bool mutliSelectedStoreInCache = false)
        //{
        //    this.Command = new ButtonCmd_ShowDataPicker().SetUrl(url).OnSelectedCallback(onSelectedCallback).OnShowCallback(onShowCallback).StoreInCache(mutliSelectedStoreInCache);

        //    return this;
        //}

        //public PageCommon_Button ShowDataPicker(string url, JObject fillConfig, string onShowCallback = "", bool mutliSelectedStoreInCache = false) 
        //{
        //    this.Command = new ButtonCmd_ShowDataPicker().SetUrl(url).FillConfig(fillConfig).OnShowCallback(onShowCallback).StoreInCache(mutliSelectedStoreInCache);

        //    return this;
        //}


        //public PageCommon_Button ExecuteCmd(string url, string cmdName, JObject args, string onSuccessCallback = "")
        //{
        //    this.Command = new ButtonCmd_ExecuteCmd();

        //    return this;
        //}


        public override HtmlString Render()
        {
            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }

            this.Css("btn-" + this._color.ToStringEx());

            this.Command?.RenderToElement(Attributes);

            ViewContext.Writer.WriteLine($"<{this.ReommentTag} {Attributes}>{_title}</{ReommentTag}>");


            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }

            return HtmlString.Empty;
        }
    }
}
